<template>
  <div class="password-update-page">
    <div class="page-title">修改密码</div>
    <form @submit.prevent="handleSubmit" :v-model="form">
      <div class="form-item"> 
        <label for="email">Email:</label>
        <input
          type="text"
          id="email"
          v-model="form.email"
          placeholder="请输入Email"
          required
        />
      </div>
      <div class="form-item">
        <label for="code">验证码:</label>
        <input
          type="text"
          id="code"
          v-model="form.code"
          placeholder="请输入验证码"
          required
        />
        <button type="button" @click="sendCode">
          {{ codeBtnText }}
        </button>
      </div>
      <div class="form-item">
        <label for="newPassword">新密码:</label>
        <input
          type="password"
          id="newPassword"
          v-model="newPassword"
          placeholder="请输入新密码"
          required
        />
      </div>
      <div class="form-item">
        <label for="confirmPassword">确认密码:</label>
        <input
          type="password"
          id="confirmPassword"
          v-model="confirmPassword"
          placeholder="请输入确认密码"
          required
        />
      </div>
      <button type="submit"  @click="handleSubmit1" class="submit-btn">保存修改</button>
    </form>
  </div>
</template>
<script setup>
import { ref,reactive } from 'vue';
import axios from '@/http/axios'
import { ElNotification } from 'element-plus'

const code = ref('');
const newPassword = ref('');
const confirmPassword = ref('');
const codeBtnText = ref('发送验证码');
const form = reactive({
  email: '',
  code:''
})
import { useLoginStore } from "@/stores/LoginStore";
const loginStore = useLoginStore();

async function sendCode() {
    //res就是服务器端返回的结果
    let res = await axios.get(`/userInfo/${form.email}`,{
      headers: {
        'Authorization': loginStore.loginToken
      }
    })
    if (res.data.code == 200) {
        ElNotification({
            title: 'Success',
            message: res.data.data,
            type: 'success',
        })
    } else {
        ElNotification({
            title: 'Error',
            message: res.data.msg,
            type: 'error',
            duration: 1000
        })
    }
}
async function handleSubmit1(){
  let res = await axios.post(
    `/userInfo/${form.email}/${form.code}/${newPassword.value}`,
    {
      headers: {
        'Authorization': loginStore.loginToken
      }
    })
  if (res.data.code == 200) {
        ElNotification({
            title: 'Success',
            message: res.data.data,
            type: 'success',
        })
        form.email = ''
        form.code = ''
        newPassword.value = ''
        confirmPassword.value = ''
    } else {
        ElNotification({
            title: 'Error',
            message: res.data.msg,
            type: 'error',
            duration: 1000
        })
    }
}

const handleSubmit = () => {
  if (form.email == '' || form.code == '' || newPassword.value == '' || confirmPassword.value == '') {
    alert('请填写完整信息');
    return;
  }
  if (newPassword.value !== confirmPassword.value) {
    alert('两次输入的密码不一致');
    return;
  }
};
</script>
<style scoped>
.password-update-page {
  width: 100%;
  margin: 0 auto;
}
.page-title {
  height: 30px;
  line-height: 30px;
  padding-left: 20px;
  font-weight: bold;
  background-color: #d3d3d3;
  margin-bottom: 100px;
}
.form-item {
  width: 45%;
  display: flex;
  align-items: center;
  margin: 0 auto;
  margin-bottom: 20px;
}
.form-item label {
  width: 100px;
  margin-right: 10px;
}
.form-item input {
  flex: 1;
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
}
.form-item button {
  margin-left: 10px;
  padding: 8px 12px;
  background-color: #fff;
  border: 1px solid #ccc;
  border-radius: 4px;
  cursor: pointer;
}
.form-item button:disabled {
  cursor: not-allowed;
  background-color: #eee;
}
.submit-btn {
  display: block;
  width: 150px;
  margin: 30px auto 0;
  padding: 10px;
  background-color: #a05c38;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
.submit-btn:hover {
  background-color: #8a4b2d;
}
</style>